<!--  -->
<template>
  <div class="send-verify">
    <!-- 头部导航 -->
    <van-nav-bar
      title="输入验证码"
      left-arrow
      fixed
      :z-index="999"
      @click-left="onClickLeft"
    >
    </van-nav-bar>
    <!-- 内容 -->
    <div class="main-content">
      <div class="tel">
        <span>验证码: </span>
        <input
          type="tel"
          maxlength="4"
          placeholder="请输入随机验证码"
          v-model="code"
          @input="handleInput"
        />
        <div class="code-btn disabled" v-if="time > 0">
          重新发送(<span>{{ time }}</span
          >)
        </div>
        <div class="code-btn" v-else @click="resendCode">重新发送</div>
      </div>
      <div class="tips">我们已经给你的手机{{telNumber}}发送了短信验证码</div>
      <div class="btn" :class="{disabled:!isVaild}" @click="next">下一步</div>
    </div>
  </div>
</template>

<script>
import { NavBar,Toast } from "vant";
import {sendCode} from '@/network/login';
export default {
  data() {
    return {
      telNumber: "18895661486",
      isVaild: false,
      //倒计时
      time: 10,
      //定时器
      timer: null,
      //验证码
      code:''
    };
  },
  //生命周期 - 创建完成（访问当前this实例）
  created() {
    // this.telNumber = this.$route.params;
    clearInterval(this.timer);
    this.timer = setInterval(() => {
      this.time--;
      if (this.time < 1) {
        this.time = 0;
        clearInterval(this.timer);
      }
    }, 1000);
  },
  //生命周期 - 挂载完成（访问DOM元素）
  mounted() {},
  methods: {
    //返回上一页
    onClickLeft() {
      this.$router.go(-1);
    },
    //验证码输入完成
    handleInput() {
      let reg = /^\d{4}$/;
      this.isVaild = reg.test(this.code);
    },
    //重新发送验证码
     resendCode() {
      this.time = 10;
      clearInterval(this.timer);
      this.timer = setInterval(() => {
        this.time--;
        if (this.time < 1) {
          this.time = 0;
          clearInterval(this.timer);
        }
      }, 1000);
      this.sendCode();
    },
    async sendCode(){
      //真正的发送请求
      try{
        const res = await sendCode(this.telNumber)
        console.log(res);
      }catch(err){
        console.log(err);
      }
    },
    //下一步
    next() {
      if (this.isVaild) {
        //执行注册用户事件
      }else{
        Toast.fail('验证码格式错误');
      }
    },
    

  },
  computed:{
   
  },
  components: {
    [NavBar.name]: NavBar,
    [Toast.name]: Toast,
  },
};
</script>
<style scoped>
.send-verify {
  padding-top: 46px;
  padding-bottom: 56px;
  font-size: 16px;
}
/* 头部导航 */
.van-nav-bar {
  background-color: #ff6a00;
}
.send-verify >>> .van-nav-bar__title {
  color: #fff;
  font-size: 18px;
}
.send-verify >>> .van-nav-bar__arrow {
  font-size: 20px;
  color: #fff;
}
.main-content {
  padding: 12px;
}
.main-content .tel {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 10px 0;
  border-bottom: 1px solid #ccc;
  white-space: nowrap;
}
.main-content .tel span {
  margin-right: 20px;
}
.main-content .tel input {
  width: 100px;
  flex: 1;
}
.main-content .tel .code-btn {
  margin-right: 20px;
  padding: 4px 8px;
  font-size: 14px;
  border-radius: 20px;
  border: 1px solid #5eb5f8;
}
.main-content .tel .code-btn.disabled {
  color: #ccc;
}
.main-content .tel .code-btn span {
  color: red;
  font-weight: bold;
  font-size: 16px;
  margin-right: 0;
}
.main-content .tips {
  font-size: 14px;
  color: #ccc;
  margin: 10px 0;
}
.main-content .btn {
  height: 44px;
  color: #fff;
  text-align: center;
  line-height: 44px;
  margin: 14px 0;
  border-radius: 20px;
  background-color: #5eb5f8;
}
.main-content .btn.disabled {
  background-color: #ccc;
}
</style>